import { stringify } from 'qs';
import _ from 'lodash';
import request, { checkResultCode, checkStatus } from '../utils/request';
import { Constants } from '../utils';
import { ipConfig } from './config';

// http://mesdev.chalco-steering.com:1708/appgateway/api-ms/pub/todo/search?appId=APP_MES&status=2&page=9&rows=10&type=1

// export const serverName = '/api-pea-device-170236';
export const serverName = ipConfig.baseServerName();

const imageUrl = `${serverName}/logistics/fileController/download/`; // 图片路径

const queryDownAccysUrl = `${serverName}/device/accyreplacement/downAccys`; // 查询待办换下备件列表

const queryMyDownAccysUrl = `${serverName}/device/accyreplacement/dowmAccys/my`; // 查询我的换下备件列表

const queryDownAccysInfoUrl = `${serverName}/device/accyreplacement/downAccys/info`; // 查询换下备件详情

const queryChildSplitsUrl = `${serverName}/device/splitItem/childSplits`; // 查询备件位置

const subChangePartUrl = `${serverName}/device/splitItem/commitAccys`; // 提交拆解

export const getDownloadImg = `${ipConfig.url(`${serverName}/logistics/fileController/download`)}`;

export const queryDownAccysList = async (params, choice) => {
  const defaultParams = { rows: Constants.PAGE_SIZE, page: 1 };
  const newParams = { ...defaultParams, ...params };
  return request(`${ipConfig.url(`${choice === 'todo' ? queryDownAccysUrl : queryMyDownAccysUrl}`)}?${stringify(newParams)}`, {
  // return request(`http://192.168.15.56:80/device/accyreplacement/dowmAccys/my?${stringify(newParams)}`, {
    method: 'GET',
  }).then(mapQueryDownAccysList);
};

export const queryDownAccysListSearch = async (params) => {
  const defaultParams = { rows: Constants.PAGE_SIZE, page: 1 };
  const newParams = { ...defaultParams, ...params };
  return request(`${ipConfig.url(queryDownAccysUrl)}?${stringify(newParams)}`, {
  // return request(`http://192.168.15.56:80/device/accyreplacement/downAccys?${stringify(newParams)}`, {
    method: 'GET',
  }).then();
};

export const queryDownAccysInfo = async (params) => {
  const newParams = { ...params };
  return request(`${ipConfig.url(queryDownAccysInfoUrl)}?${stringify(newParams)}`, {
  // return request(`http://192.168.15.56:80/device/accyreplacement/downAccys/info?${stringify(newParams)}`, {
    method: 'GET',
  }).then(response => response.data);
};

export const queryChildSplits = async (params) => {
  const newParams = { ...params };
  return request(`${ipConfig.url(queryChildSplitsUrl)}?${stringify(newParams)}`, {
  // return request(`http://192.168.15.56:80/device/splitItem/childSplits?${stringify(newParams)}`, {
    method: 'GET',
  }).then();
};

export const subChangePart = async params => {
  return request(`${ipConfig.url(subChangePartUrl)}`, {
  // return request('http://192.168.15.56:80/device/splitItem/commitAccys', {
    method: 'POST',
    body: params,
  }).then(response => response.data);
};

const mapQueryDownAccysList = response => {
  const data = _.map(response.data, it => ({
    materialName: it.materialName,
    equipmentName: it.equipmentName,
    materialCode: it.materialCode,
    lotCode: it.lotCode,
    failureCode: it.failureCode,
    failurePic: it.equipmentImage ? `${ipConfig.url(imageUrl)}${it.equipmentImage}?token=${global.__TOKEN_ACCESS__}` : 'no-pic',
  }));

  return {
    list: data,
    page: response.pageNum,
    total: response.totalNum,
  };
};
